home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / (A)P / (A)P1.ADF / Polygon / iffwriter / iffwriter2.c < prev    next >
C/C++ Source or Header  |  1987-05-25  |  4KB  |  139 lines

  1. /*****************************************************************************
  2.  *           Copyright (C) 1986, =Robert J. Mical=
  3.  *                Placed in the Public Domain
  4.  ****************************************************************************/
  5. #include "iffwriter.h"
  6.  
  7. extern struct   RastPort      *rp;
  8. extern struct   ViewPort      *vp;
  9.  
  10. extern struct   Window  *w;
  11. extern struct   Screen  *screen;
  12.  
  13. extern struct NewScreen ns;
  14.  
  15. LONG PictModes[3] =
  16.     {
  17.     CAMG,
  18.     4,
  19.     NULL,
  20.     };
  21.  
  22. LONG PictBody[2] =
  23.     {
  24.     BODY,
  25.     40000,
  26.     };
  27.  
  28.  
  29. struct PaintingHeader PaintingHeader =
  30.     {
  31.     FORM,               /* IFFID */
  32.     (40156),            /* file length */
  33.     ILBM,               
  34.     BMHD,               
  35.     sizeof(struct BitMapHeader),
  36.         {               /* start of the BitMapHeader structure */
  37.         320, 200,
  38.         0, 0,
  39.         5,
  40.         0, 
  41.         0,
  42.         0,
  43.         0,
  44.         10, 11,
  45.         320, 200,       /* end of BitMapHead */
  46.         },
  47.     CMAP,               
  48.     96,                 /* this value is followed by 32 color registers */
  49. };
  50. char filename[32]="temp.pic";
  51.  
  52. BOOL SavePicture()
  53. {
  54.     SHORT i, j, offset;
  55.     ULONG length, actual_length;
  56.     UBYTE *p, component, rgbbyte;
  57.     USHORT rgb;
  58.     BOOL written;
  59.     int file,n;
  60.  
  61.     written = FALSE;
  62.     /* Open the data file */
  63.     file = open(filename,O_WRONLY | O_TRUNC | O_CREAT);
  64.     if (file == -1)
  65.         goto EXIT_ERROR;
  66.  
  67.     /***********************************************************************
  68.      * to write the buffer to the disk, we need to write:
  69.      *      "FORM"
  70.      *      file length
  71.      *      "ILBM"
  72.      *      "BMHD"
  73.      *      BMHD length
  74.      *      BitMapHeader structure
  75.      *      "CMAP"
  76.      *      CMAP length
  77.      *      colors
  78.      *      pad byte if needed
  79.      *      "BODY"
  80.      *      BODY length
  81.      *      body data
  82.      **********************************************************************/
  83.  
  84.      /* First, write out the header */
  85.     length = sizeof(struct PaintingHeader);
  86.     actual_length = write(file, &PaintingHeader, length);
  87.     if (actual_length != length)
  88.         goto WRITE_ERROR;
  89.  
  90.     /* also, enough bytes for each color register */
  91.     for (i = 0; i < (1 << ns.Depth); i++)
  92.         {
  93.         for (component = 0; component < 3; component++)
  94.             {
  95.             rgb = (GetRGB4(vp->ColorMap, i));
  96.             rgbbyte = (rgb >> (4 * (2 - component)));
  97.             rgbbyte <<= 4;
  98.             actual_length = write(file, &rgbbyte, 1);
  99.             if (actual_length != 1)
  100.                 goto WRITE_ERROR;
  101.             }
  102.         }
  103.     actual_length = write(file,  &PictBody[0], 8);
  104.     if (actual_length != 8)
  105.         goto WRITE_ERROR;
  106.  
  107.     /* Finally, the data (uncompressed for now). */
  108.     /* First, get the bytelength per line */
  109.     length = (((ns.Width + 15) >> 4) << 1);
  110.  
  111.     /* For every line ... */
  112.     for (j = 0; j < ns.Height; j++)
  113.         {
  114.         offset = length * j;
  115.         /* For every bit plane ... */
  116.         for (i = 0; i < ns.Depth; i++)
  117.             {
  118.             /* p is set to the start of the line in this bit plane */
  119.             p = (screen->BitMap.Planes[i]) + offset;
  120.             actual_length = write(file, p, length);
  121.             if (actual_length != length) goto WRITE_ERROR;
  122.             }
  123.         }
  124.  
  125.     PictModes[2] = vp->Modes;
  126.     actual_length = write(file,  &PictModes[0], 12);
  127.     if (actual_length != 12)
  128.         goto WRITE_ERROR;
  129.     written = TRUE;
  130.  
  131. WRITE_ERROR:
  132.     close(file);
  133.  
  134. EXIT_ERROR:
  135.     for (n=1;n<=500000;n++)
  136.     ;
  137.     return(written);
  138. }
  139.